package com.zkingsoft.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebMsg;
import com.zkingsoft.web.service.WebMsgService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-01-11 13:38
 */
@Controller
@RequestMapping(value = "admin/webMsg")
public class WebMsgController {

	@Resource
	private WebMsgService webMsgService;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "WebMsg_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebMsg webMsg, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webMsg.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,  
				webMsgService.findInPage(webMsg, pageVo), webMsgService.findTotal(webMsg));
		return result;
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebMsg webMsg) {
		if (webMsg.getMsgId() != null) {
			webMsg.setMsgId(((WebMsg) WebUtil.getSessionAttribute(BEV)).getMsgId());
			int i = webMsgService.modifyByMap(WebUtil.getSessionAttribute(BEV), webMsg);
			WebUtil.removeSessionAttribute(BEV);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "修改失败");
			}
		} else {
			SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
			webMsg.setCompanyId(user.getCompanyId());
			int i = webMsgService.add(webMsg);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "新增失败");
			}
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebMsg webMsg;
		if (id != null) {
			webMsg = webMsgService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webMsg);
			WebUtil.setSessionAttribute(BEV, webMsg);
		}
		return "admin/webMsg-form";
	}

	/**
	 * 删除
 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webMsgService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,  "删除失败");
		}
	}
	
}